package com.hanlink.dsp.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.qztech.sdk.R;


/**
 * Created by zhb on 2015-11-23.
 */
public class MessageDialog extends Dialog {

    private TextView textView;
    private ImageView ivprogress;
    private Animation animation;

    public MessageDialog(Context context) {
        super(context, R.style.dialog);
        initView();
        this.show();
    }

    public MessageDialog(Context context, int resStringId, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        initView();
    }

    private void initView() {
        WindowManager wm = (WindowManager) getContext().getSystemService(
                Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        setContentView(getLayoutInflater().inflate(R.layout.dialog_bluetooth, null), new ViewGroup.LayoutParams((int) (outMetrics.widthPixels * 0.9), ViewGroup.LayoutParams.WRAP_CONTENT));
        if (getWindow() != null) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        }
        this.textView = (TextView) findViewById(R.id.txt_bluetooth);
        this.ivprogress = (ImageView) findViewById(R.id.iv_progress);
        animation = AnimationUtils.loadAnimation(getContext(), R.anim.loading_bluetooth);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    @Override
    public void show() {
        if (ivprogress != null) {
            ivprogress.setImageResource(com.hanlink.res.R.drawable.loading);
//            ivprogress.startAnimation(animation);
            final Drawable drawable = ivprogress.getDrawable();
            if (drawable != null && drawable instanceof AnimationDrawable){
                ((AnimationDrawable)drawable).start();
            }
        }
        super.show();
    }

    @Override
    public void dismiss() {
        if (ivprogress != null) {
            ivprogress.setAnimation(null);
        }
        super.dismiss();
    }

    public void setMessage(int resId) {
        textView.setText(getContext().getString(resId));
    }

}
